#!/bin/bash

# WHAT	: eti_register
# WHY	: Run this script to install automator key and grab information
# WHEN	: July 10, 2007
# WHO	: Mohit Suley, TCS 
# NOTES	:
# FUNCS	: ------------------------------------------------------------------
#	: alert() - Stdout function if a log is need that is. 
#	: prerun_check()- Check for root account etc
#	: install_key() - Install the Automator key in authorized_keys for root
#	: get_primary_ip() - Extract the Primary ethernet interface, assumed to be eth0
#	: get_host_info() - Get all uname info
#	: show_intro() - Show startup message
#	: get_application() - Ask user for application hosted on this server
#	: get_geography() - Global location for this server. Datacenters can be added as well
#	: get_environment() - Dev, QA, Stage, Prod or Test environment
#	: get_tier() - App, Web, Web-App, DB or a test box 
#	: get_type() - Is it a standalone, VZ or VMware box
#	: post_info() - Goodbye message
# ----------------------------------------------------------------------------------------------------

# set -x

ETI_LOGNAME=/dev/null

SSHKEY="ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAIEAsAPcOmnZeq4UdQDny4Run1WZXdxRO8NVnFCcL2uydPUKAUOa+qUFYkb+xnf3WTlY8LDg+HzMwWF20W0h2RBC4GNSP7pfvbLYqE1Wq6c44M7gWp31FrRsQJ7cmjtZ0sRkJUKa8dF0F3N1rYBciVjJpP6NlaNPCNh40i/cl+hcQu0= automator@cihcispweb220"

AUTHKEYSFILE=/root/.ssh/authorized_keys

# Error codes
E_NOTROOT=100
E_KEYEXISTS=101

function alert
{
        if [ -z $ETI_LOGNAME ]; then
        {
                ETI_LOGNAME=/dev/stdout
        }
        fi

        echo `date` === "$1" >> $ETI_LOGNAME

}


function prerun_check
{
	# Check if running as root
	if [ "`whoami`" != "root" ]; then
	{
		echo "Run script as root!!"
		exit $E_NOTROOT
	}
	fi

}

function install_key
{
	# Install key in authorized_keys
	
	if [! -d /root/.ssh ]; then
	        {
	                alert ".ssh didn't exist on `hostname`"
	                mkdir /root/root/.ssh
	                chmod 600 /root/.ssh
        	}
	fi

	# check if key already added, dont want trash in this file 

	if [ -z `grep "automator@cihcispweb220" $AUTHKEYSFILE` ]; then
		alert "Installing Automator authorized key on `hostname`"
		echo $SSHKEY >> $AUTHKEYSFILE
	fi
}



function get_application
{
	clear
	cat <<- end-of-app-list

	Application (the application this server is primarily used for) : 
	---------------------

	[1] InsideGE		[2] SharedVAP		[3] Virtuozzo
	[4] GE.com		[5] VBCS		[6] Search
	[7] MySQL		[8] NewMedia		[9] Teamsite
	[10] Breeze		[11] SetPAC		[12] Integration
	[13] General-PoC	[14] Subversion		[15] Wiki
	[16] CA Introscope	[17] GE Careers		[18] Jira
	[19] Nagios		[20] Other...
	
	---------------------

	Enter your option [1..20] : 

	end-of-app-list

	read APPNUMBER
	
	case $APPNUMBER in
		1)	APPNAME=INSIDEGE
		;;
		2)	APPNAME=SHAREDVAP
		;;
		3)	APPNAME=VIRTUOZZO
		;;
		4)	APPNAME=GE.COM
		;;
		5)	APPNAME=VBCS
		;;
		6)	APPNAME=SEARCH
		;;
		7)	APPNAME=MYSQL
		;;
		8)      APPNAME=NEWMEDIA
	        ;;
		9)      APPNAME=TEAMSITE
	        ;;
		10)      APPNAME=BREEZE
	        ;;
		11)      APPNAME=SETPAC
		;;
		12)      APPNAME=INTEGRATION
	        ;;
		13)      APPNAME=POC
	        ;;
		14)      APPNAME=SUBVERSION
	        ;;
		15)      APPNAME=WIKI
	        ;;
		16)      APPNAME=INTROSCOPE
	        ;;
		17)      APPNAME=CAREERS
		;;
		18)      APPNAME=JIRA
	        ;;
		19)      APPNAME=NAGIOS
	        ;;
		20)      echo "Please make appropriate entry in /tmp/$IP_ADDR.conf after all other details have been filled in." 
	        ;;
	esac
	
	export APPNAME
	echo
	#echo "You entered $APPNAME as the primary application hosted on this box, press N/n if incorrect:"
	#read -n 1 incorrect

	#case $incorrect in
	#[nN])	get_application
	#	;;
	#esac

	echo "APPNAME=$APPNAME" >> $CONF_FILE

}


function get_geography
{
	clear
	cat <<-end-of-geo-list
	
	Global Location (Geography, where the server is located): 
	---------------------
	
	[1] USA		[2] Asia-Pacific	[3] Europe
	[4] Other...

	---------------------

	Enter your option [1..4]: 

	end-of-geo-list
	
	read GEONUMBER
	
	case $GEONUMBER in
		1)	GEONAME=USA
		;;
		2)	GEONAME=ASPAC
		;;
		3)	GEONAME=EUROPE
		;;
		4)	ECHO "Please make appropriate entry in /tmp/$IP_ADDR.conf after all other details have been filled in."
		;;
	esac
	
	export GEONAME
	echo
	echo "GEONAME=$GEONAME" >> $CONF_FILE

}

function get_environment
{
        clear
        cat <<-end-of-env-list

	Environment:
	---------------------

	[1] Development		[2] QA		[3] Stage
	[4] Production		[5] Test	[6] POC

	---------------------

	Enter your option [1..6]:

	end-of-env-list

        read ENVNUMBER

        case $ENVNUMBER in
                1)      ENVNAME=DEV
                ;;
                2)      ENVNAME=QA
                ;;
                3)      ENVNAME=STAGE
                ;;
		4)      ENVNAME=PROD
                ;;
		5)      ENVNAME=TEST
		;;
		6)      ENVNAME=POC
                ;;
        esac

        export ENVNAME
        echo
        echo "ENVNAME=$ENVNAME" >> $CONF_FILE

}

function get_tier
{
        clear
        cat <<-end-of-tier-list

	Server Tier:
	---------------------

	[1] Web		[2] Application		[3] Database
	[4] VersionCtrl [5] WEB-APP		[6] Hosting

	---------------------

	Enter your option [1..6]:

	end-of-tier-list

        read TIERNUMBER

        case $TIERNUMBER in
                1)      TIERNAME=WEB
                ;;
                2)      TIERNAME=APP
                ;;
                3)      TIERNAME=DB
                ;;
                4)      TIERNAME=VERSION
                ;;
                5)      TIERNAME=WEBAPP
                ;;
                6)      TIERNAME=HOSTING
                ;;
        esac

        export TIERNAME
        echo "TIERNAME=$TIERNAME" >> $CONF_FILE

}

function get_type
{
	clear
	cat<<-end-of-type-list
	
	Server Type:
	---------------------
	
	[1] Standalone		[2] Virtuozzo Instance
	[3] VMWare Machine	[4] Xen Domain

	---------------------

	Enter your option [1..4]: 

	end-of-type-list

	read TYPENUMBER

	case $TYPENUMBER in
		1)	TYPE=STANDALONE
		;;
		2)	TYPE=VIRTUOZZO
		;;
		3)	TYPE=VMWARE
		;;
		4)	TYPE=XEN
		;;
	esac

	export TYPE
	echo "TYPE=$TYPE" >> $CONF_FILE
}


function get_primary_ip
{
        alert "Grabbing Primary IP Address"
        export IP_ADDR=`/sbin/ifconfig eth0 | grep "inet addr" | awk -F ":" '{print $2}' | awk '{print $1}'`
        alert "IP Address is : $IP_ADDR"
        
	export CONF_FILE=/tmp/$IP_ADDR.conf

	if [ -f $CONF_FILE ]; then 
		rm -f $CONF_FILE
	fi
}


function get_host_info
{
        echo "HOSTNAME=`hostname`" >> $CONF_FILE
        echo "FQDN=`hostname -f`" >> $CONF_FILE
        echo "KERNEL=`uname -r`" >> $CONF_FILE
        echo "OS=`uname -s`" >> $CONF_FILE
        echo "ARCH=`uname -m`" >> $CONF_FILE
        alert "Added hostname, fqdn, kernel, os and arch"
}


function show_intro
{
        clear
        cat <<-end-of-intro


        Welcome to Automator!
        ---------------------

	This version of the script is only for Linux.

        Information retrieved from the box:

        PRIMARY IP ADDRESS      :       $IP_ADDR

        FQDN    :       `hostname -f`
        KERNEL  :       `uname -r`
        OS      :       `uname -s`
        ARCH    :       `uname -m`

        ---------------------

end-of-intro


        echo
        echo
        echo "Press ENTER to continue, Control-C to exit..."
        read dummykey
}

function post_info
{
	clear
	
	echo
	echo "All required information copied to /tmp/${IP_ADDR}.conf . "
	echo ------- "${IP_ADDR}.conf" -------
	echo
	cat /tmp/${IP_ADDR}.conf

	cat<<-end-of-post-info
	
	You can change this file in /tmp/${IP_ADDR}.conf in case the information is not correct, or re-run this script. 

	Please add the IP ${IP_ADDR} to your Automator server in AUTOMATOR_HOME/new_registrations. You will be able to use all services then. 

	Thank you for your co-operation. 


	end-of-post-info
	
	sleep 3
	
}




function main
{
	prerun_check
	install_key
	get_primary_ip
	get_host_info
	show_intro
	get_application
	get_geography
	get_environment
	get_tier
	get_type
	post_info
	exit 
}



main


